package com.ykt.validator;

import com.jfinal.core.Controller;
import com.ykt.common.utils.ObjectUtil;
import com.ykt.model.Order;

/**
 * 通用参数校验
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-10 22:18
 */
public class CommonValidator{


    /**只校验用户手机号*/
    public static class PhoneValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateRequired("phone", "phoneReqMsg", "手机号不能为空！");
            validateRegex("phone", "1[345678]\\d{9}", "phoneRegMsg", "手机号格式不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("phone");
            c.renderJson(super.toJsonText(c));
        }
    }


    /**只校验用户ID*/
    public static class UserIdValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateInteger("userId", "userIdReqMsg", "用户ID不能为空！");
            validateRegex("userId", "\\d{1,11}", "userIdRegMsg", "用户ID格式不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("userId");
            c.renderJson(super.toJsonText(c));
        }
    }

    /**只校验商户ID*/
    public static class ShopIdValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateInteger("shopId", "shopIdReqMsg", "商户ID不能为空！");
            validateRegex("shopId", "\\d{1,11}", "shopIdRegMsg", "商户ID格式不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("shopId");
            c.renderJson(super.toJsonText(c));
        }
    }

    /**只校验市场ID*/
    public static class MarketIdValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateInteger("marketId", "marketIdReqMsg", "市场ID不能为空！");
            validateRegex("marketId", "\\d{1,11}", "marketIdRegMsg", "市场ID格式不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("marketId");
            c.renderJson(super.toJsonText(c));
        }
    }

    /**经纬度校验*/
    public static class LonLatValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateDouble("longitude", "longitudeMsg", "经度参数不能为空！");
            validateDouble("latitude", "latitudeMsg", "纬度参数不能为空！");
        }

        @Override
        protected void handleError(Controller c) {
            c.renderJson(super.toJsonText(c));
        }

    }


    /**只校验订单ID*/
    public static class OrderIdValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            validateInteger("orderId", "orderIdReqMsg", "订单ID不能为空！");
            validateRegex("orderId", "\\d{1,11}", "orderIdRegMsg", "订单ID格式不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("orderId");
            c.renderJson(super.toJsonText(c));
        }
    }

    /**只校验订单状态*/
    public static class OrderStatusValidator extends BaseValidator{

        @Override
        protected void validate(Controller c) {
            String[] values = ObjectUtil.toArray(Order.Status.values());
            validateInSet("orderStatus", values, "orderStatusMsg", "订单状态取值不正确！");
        }

        @Override
        protected void handleError(Controller c) {
            c.keepPara("orderStatus");
            c.renderJson(super.toJsonText(c));
        }
    }
}
